/*
 * main.cpp
 *
 *  Created on: Nov 12, 2012
 *      Author: monitz
 */

#include <fstream>
using namespace std;
#include <iostream>
#include <sstream>
#include <vector>
#include <stdlib.h>

int createCourse(Course& course,string& department,string& name,int& minimum)
{
	if(department=="ELECTIVE")
	{
		Course=new El_course(name,minimum);
		return 0;
	}
	if(department=="CS")
	{
		Course=new CS_course(name,minimum);
		return 1;
	}
	if(department=="PG")
	{
		Course=new PG_course(name,minimum);
		return 2;
	}

}

int parseDepartments(vector <vector <Course*> >& courses)
{
	string line;
	int numOfDepartments=1;
	string departmentName;
	ifstream fh(".conf");
	if (fh.is_open()) {
		while (fh.good()) {
			getline(fh, line);
			if(numOfDepartments==1)	//first time, get number of semesters for uni
			line = line.substr(line.find_first_of('=') + 1, line.size() );
			else
				{
				//parse department name
				departmentName=line.substr(0, line.find_first_of(','));
				line = line.substr(line.find_first_of(',') + 1, line.size());
				//parse minimum
				minimum = atoi(line.substr(0, line.find_first_of(',')).c_str());
				line = line.substr(0, line.find_first_of(',') + 1);
				//parse semester
				semester = atoi(line.substr(0, line.find_first_of(',')).c_str());
				line = line.substr(0, line.find_first_of(',') + 1);
				//create Course object
				depLoc=createCourse(newCourse,department,courseName,semester,minimum);
				//enter course to the right place at vector
				courses.at(depLoc);
				}
			}
			fh.close();
		}
		else
			cout << "Unable to open file departments";
}

void parseCourses(vector <vector <Course*> >& courses) {
	string line;
	string department,courseName;
	int semester;
	int minimum;
	int depLoc;
	ifstream fh("courses.conf");
	if (fh.is_open()) {
		while (fh.good()) {
			Course newCourse;
			getline(fh, line);
			//parse department
			department=line.substr(0, line.find_first_of(','));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//parse course name
			courseName=line.substr(0, line.find_first_of(','));
			line = line.substr(line.find_first_of(',') + 1, line.size());
			//parse minimum
			minimum = atoi(line.substr(0, line.find_first_of(',')).c_str());
			line = line.substr(0, line.find_first_of(',') + 1);
			//parse semester
			semester = atoi(line.substr(0, line.find_first_of(',')).c_str());
			line = line.substr(0, line.find_first_of(',') + 1);
			//create Course object
			depLoc=createCourse(newCourse,department,courseName,semester,minimum);
			//enter course to the right place at vector
			courses.at(depLoc);
		}
		fh.close();
	}
	else
		cout << "Unable to open file courses";
}

int main(int argc, char *argv[]) {

	/////////////////////////////////////////////////
	// DATABASE CONFIG:
	int UniSemesters;
	vector<vector<Course*> > *courseVec;
	courseVec= parseDepartment();
	parseCourses(courseVec);
	vector<vector<Student*> > *studentVec = parseStudents();

	/////////////////////////////////////////////////////
	//random initialized:
	srand(time(0));
	/////////////////////////////////////////////////////

	ofstream fhs("CS.out");
	ofstream fhc("PG.out");
	//go through the courses, print the enrolled students into file
	for (unsigned int j = 0; j < courseVec->size(); j++) {
		//print course header
		fhc << courseVec->at(j).at(0) << " " << courseVec->at(j).at(1) << endl;
		for (unsigned int i = 2; i < courseVec->at(j).size(); i++)
			//print non-empty enrolled students
			if (courseVec->at(j).at(i).compare("") == 0)
				i = courseVec->at(j).size();
			else
				fhc << '\t' << courseVec->at(j).at(i) << endl;
	}
	//go through the students, print the courses the student is enrolled to into file
	for (unsigned int j = 0; j < studVec->size(); j++) {
		//go through the seven weekday, print courses
		for (unsigned int i = 0; i < studVec->at(j).size(); i++) {
			fhs << studVec->at(j).at(i);
		}
		fhs << endl;
	}
	delete courseVec;
	delete studVec;
	fhs.close();
	fhc.close();
}

